#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QNetworkInterface"

#include "QHostInfo"

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 点击后获取主机信息
    connect(ui->button_HostInfo, &QPushButton::clicked, this, [this]() {
        ui->plainTextEdit_Host->clear();
        // 获取当前电脑的主机名称
        QString hostName = QHostInfo::localHostName();
        ui->plainTextEdit_Host->appendPlainText("主机名称  " + hostName + "\n");
        // 通过 DNS 查询 主机名（网络操作）
        QHostInfo hostInfo = QHostInfo::fromName(hostName);
        QList<QHostAddress> addrList = hostInfo.addresses();
        if (addrList.isEmpty())
            return;
        for (const auto& list : addrList)
        {
            if (list.protocol() == QAbstractSocket::IPv4Protocol)
            {
                ui->plainTextEdit_Host->appendPlainText("协议  IPv4");
            }
            else if (list.protocol() == QAbstractSocket::IPv6Protocol)
            {
                ui->plainTextEdit_Host->appendPlainText("协议  IPv6");
            }
            else
            {
                ui->plainTextEdit_Host->appendPlainText("协议  ????");
            }
            // 将内部存储的 IP 地址（IPv4 或 IPv6）格式化为标准的字符串表示形式。
            // IPv4 标准点分十进制格式
            // IPv6 标准冒号十六进制格式，自动压缩连续的零
            ui->plainTextEdit_Host->appendPlainText(list.toString());

            // 判断一个 IP 地址是否是全局单播地址（Global Unicast Address）。
            if (list.isGlobal())
            {
                ui->plainTextEdit_Host->appendPlainText("在公共互联网上路由  是");
            }
            else
            {
                ui->plainTextEdit_Host->appendPlainText("在公共互联网上路由  否");
            }
            ui->plainTextEdit_Host->appendPlainText("\n");
        }
    });

    connect(ui->button_Lookup, &QPushButton::clicked, this, [this]() {
        ui->plainTextEdit_Lookup->clear();
        QString name = ui->lineEdit->text();
        // 域名是类似 cn.bing.com 的东西，详情见笔记
        ui->plainTextEdit_Lookup->appendPlainText("要查询的域名  " + name + "\n");
        // 通过 DNS 查询 主机名（网络操作）
        QHostInfo info = QHostInfo::fromName(name);
        QList<QHostAddress> addrList = info.addresses();
        if (addrList.isEmpty())
            return;

        for (const auto& list : addrList)
        {
            if (list.protocol() == QAbstractSocket::IPv4Protocol)
            {
                ui->plainTextEdit_Lookup->appendPlainText("协议  IPv4");
            }
            else if (list.protocol() == QAbstractSocket::IPv6Protocol)
            {
                ui->plainTextEdit_Lookup->appendPlainText("协议  IPv6");
            }
            else
            {
                ui->plainTextEdit_Lookup->appendPlainText("协议  ????");
            }
            // 将内部存储的 IP 地址（IPv4 或 IPv6）格式化为标准的字符串表示形式。
            // IPv4 标准点分十进制格式
            // IPv6 标准冒号十六进制格式，自动压缩连续的零
            ui->plainTextEdit_Lookup->appendPlainText(list.toString());

            // 判断一个 IP 地址是否是全局单播地址（Global Unicast Address）。
            if (list.isGlobal())
            {
                ui->plainTextEdit_Lookup->appendPlainText("在公共互联网上路由  是");
            }
            else
            {
                ui->plainTextEdit_Lookup->appendPlainText("在公共互联网上路由  否");
            }
            ui->plainTextEdit_Lookup->appendPlainText("\n");
        }
    });

    connect(ui->button_AllAddress, &QPushButton::clicked, [this]() {
        ui->plainTextEdit_AllAddress->clear();

        ui->plainTextEdit_AllAddress->appendPlainText("获取本机 IP 地址\n");
        // 直接读取本机网卡信息（本地操作）
        QList<QHostAddress> addrList = QNetworkInterface::allAddresses();
        if (addrList.isEmpty())
            return;

        for (const auto& list : addrList)
        {
            if (list.protocol() == QAbstractSocket::IPv4Protocol)
            {
                ui->plainTextEdit_AllAddress->appendPlainText("协议  IPv4");
            }
            else if (list.protocol() == QAbstractSocket::IPv6Protocol)
            {
                ui->plainTextEdit_AllAddress->appendPlainText("协议  IPv6");
            }
            else
            {
                ui->plainTextEdit_AllAddress->appendPlainText("协议  ????");
            }
            // 将内部存储的 IP 地址（IPv4 或 IPv6）格式化为标准的字符串表示形式。
            // IPv4 标准点分十进制格式
            // IPv6 标准冒号十六进制格式，自动压缩连续的零
            ui->plainTextEdit_AllAddress->appendPlainText(list.toString());

            // 判断一个 IP 地址是否是全局单播地址（Global Unicast Address）。
            if (list.isGlobal())
            {
                ui->plainTextEdit_AllAddress->appendPlainText("在公共互联网上路由  是");
            }
            else
            {
                ui->plainTextEdit_AllAddress->appendPlainText("在公共互联网上路由  否");
            }
            ui->plainTextEdit_AllAddress->appendPlainText("\n");
        }
    });

    connect(ui->button_AllInterface, &QPushButton::clicked, this, [this]() {
        ui->plainTextEdit_AllInterface->clear();
        QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
        for (const auto& item : list)
        {
            if (!item.isValid())
                continue;

            ui->plainTextEdit_AllInterface->appendPlainText("设备名称  " + item.humanReadableName());
            ui->plainTextEdit_AllInterface->appendPlainText("硬件地址  " + item.hardwareAddress());

            QString theType;

            switch (item.type())
            {
                /*常见*/
            case QNetworkInterface::Loopback:
                theType = "回环接口";
                break;
            case QNetworkInterface::Virtual:
                theType = "虚拟网络接口";
                break;
            case QNetworkInterface::Ethernet:
                theType = "以太网接口";
                break;
            case QNetworkInterface::Wifi:
                theType = "无线局域网";
                break;

                /*较少见*/
            case QNetworkInterface::Ppp:
                theType = "点对点协议";
                break;

                /*以下罕见*/
            case QNetworkInterface::Slip:
                theType = "串行线路 IP";
                break;
            case QNetworkInterface::CanBus:
                theType = "控制器局域网总线";
                break;

            case QNetworkInterface::Fddi:
                theType = "光纤分布式数据接口";
                break;

            case QNetworkInterface::Phonet:
                theType = "PhoNet 协议";
                break;
            case QNetworkInterface::Ieee802154:
                theType = "低速无线个域网";
                break;
            case QNetworkInterface::SixLoWPAN:
                theType = "IPv6 低功耗无线个域网";
                break;
            case QNetworkInterface::Ieee80216:
                theType = "WiMAX";
                break;
            case QNetworkInterface::Ieee1394:
                theType = "火线（FireWire）";
                break;
            default:
                break;
            }

            ui->plainTextEdit_AllInterface->appendPlainText("网络接口  " + theType);

            QList<QNetworkAddressEntry> entryList = item.addressEntries();
            for (const auto& entry : entryList)
            {
                ui->plainTextEdit_AllInterface->appendPlainText("IP地址  " + entry.ip().toString());
                ui->plainTextEdit_AllInterface->appendPlainText("子网掩码  " + entry.netmask().toString());
                ui->plainTextEdit_AllInterface->appendPlainText("广播地址  " + entry.broadcast().toString());
            }

            ui->plainTextEdit_AllInterface->appendPlainText("\n");
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}
